// pages/buy/index.js
import Config from '../../etc/config'
import util from '../../utils/util'
const {
  $Toast
} = require('../../components/dist/base/index');
var dateTimePicker = require('../../utils/dateTimePicker.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    collList: [],
    merTypeId: '',
    shopId: '',
    addressId: '',
    ageList: [],
    age: 20,
    ageLeft: 0,
    stature: 110,
    statureList: [],
    statureLeft: 0,
    weight: 30,
    weightList: [],
    weightLeft: 0,
    money: 0,
    disabled: false,
    brandDis: true,
    dateTime: [],
    startYear: 2000,
    endYear: 2050
  },

  getAllShop() {
    let params = {
      typeId: this.data.merTypeId,
    }
    let token = wx.getStorageSync("token");
    Config._post('/shop/getShopByTypeId', token, params).then(res => {
      this.setData({
        shopList: res.data
      })
    })
  },

  // 选择门店
  bindShopChange(e) {
    this.setData({
      shopsIndex: e.detail.value,
      shopId: this.data.shopList[e.detail.value].id
    })
    this.getBrands(this.data.shopList[e.detail.value].id)
  },

  // 获取门店对应品牌
  getBrands(id) {
    let params = {
      shopId: id,
    }
    let token = wx.getStorageSync("token");
    Config._post('/shop/getBrandByShopId', token, params).then(res => {
      this.setData({
        brandsList: res.data,
        brandDis: false
      })
    })
  },

  // 选择品牌
  bindBrandChange(e) {
    this.setData({
      brandIndex: e.detail.value,
      brandId: this.data.brandsList[e.detail.value].id
    })
  },

  bindDateChange(e) {
    // this.setData({
    //   date: e.detail.value
    // })
    let idx = e.detail.value
    let data = this.data.dateTime[0][idx[0]] + '-' + this.data.dateTime[1][idx[1]] + '-' + this.data.dateTime[2][idx[2]] + ' ' + this.data.dateTime[3][idx[3]] + ':' + this.data.dateTime[4][idx[4]]
    this.setData({
      date: data
    })
  },

  // 下单
  orderSubmit: function(e) {
    this.setData({
      disabled: true
    })
    let formId = e.detail.formId
    if (this.data.money == 0) {
      $Toast({
        content: '请填写预算资金'
      })
      this.setData({
        disabled: false
      })
      return
    }
    if (!this.data.shopId) {
      $Toast({
        content: '请选择预约门店'
      })
      this.setData({
        disabled: false
      })
      return
    }
    if (!this.data.name) {
      $Toast({
        content: '请填写联系人'
      })
      this.setData({
        disabled: false
      })
      return
    }
    if (!this.data.phone) {
      $Toast({
        content: '请填写联系电话'
      })
      this.setData({
        disabled: false
      })
      return
    }
    let params = {
      userId: wx.getStorageSync('userId'),
      merTypeId: this.data.merTypeId,
      shopId: this.data.shopId,
      orderMember: this.data.name,
      orderMobile: this.data.phone,
      formId: formId,
      bTime: Date.parse(this.data.date),
      brandId: this.data.brandId,
      orderBrand: this.data.brandsList[this.data.brandIndex].name
    }
    let checkItems = getApp().globalData.collList
    var value = '',
      val = ''
    for (let i = 0; i < checkItems.length; i++) {
      for (let j = 0; j < checkItems[i].value.length; j++) {
        if (j == checkItems[i].value.length - 1) {
          val = checkItems[i].value[j]
        } else {
          val = checkItems[i].value[j] + '、'
        }
      }
      value += checkItems[i].name + '：' + val + '，'
    }
    params.buyerRemake = '年龄：' + this.data.ageList[this.data.age] + '岁，' + '身高：' + this.data.statureList[this.data.stature] + 'cm，' + '体重：' + this.data.weightList[this.data.weight] + 'kg，' + value + '预算金额：' + this.data.money + '元'
    let token = wx.getStorageSync("token");
    Config._post('/order/submitOrder', token, params).then(res => {
      console.log(res)
      this.setData({
        disabled: false
      })
      wx.reLaunch({
        url: '../success-msg/index',
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    for (let i = 2; i <= 100; i++) {
      this.data.ageList.push(i)
    }
    for (let i = 50; i <= 240; i++) {
      this.data.statureList.push(i)
    }
    for (let i = 10; i <= 100; i++) {
      this.data.weightList.push(i)
    }
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var lastArray = obj.dateTimeArray.pop();
    var lastTime = obj.dateTime.pop();
    this.setData({
      start: obj.dateTime,
      dateTime: obj.dateTimeArray,
      ageList: this.data.ageList,
      statureList: this.data.statureList,
      weightList: this.data.weightList,
      merTypeId: options.merTypeId
    })
    this.getDefault()
    this.getAllShop()
    var that = this
    wx.createSelectorQuery().select('#age1').fields({
      size: true,
      rect: true
    }, function(res) {
      that.data.value = res.width
      that.data.coordinates = res.right - res.left
      that.setData({
        ageLeft: -(that.data.age - 3) * res.width,
        statureLeft: -(that.data.stature - 3) * res.width,
        weightLeft: -(that.data.weight - 3) * res.width
      })
      that.data.ageLeft = 0
      that.data.statureLeft = 0
      that.data.weightLeft = 0
    }).exec()
  },

  getDefault(){
    let params = {
      userId: wx.getStorageSync('userId'),
    }
    let token = wx.getStorageSync("token");
    Config._post('/order/dapeiDefault', token, params).then(res => {
      this.setData({
        phone: res.data.orderMobile,
        name: res.data.orderMember
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.animation = wx.createAnimation()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      // title: wx.getStorageSync('mallName') + '——' + app.globalData.shareProfile,
      title: app.globalData.shareTitle,
      path: '/pages/collocation/index?type=' + this.data.type + '&typeId=' + this.data.typeId,
      imageUrl: this.data.img,
      success: function(res) {
        // 转发成功
      },
      fail: function(res) {
        // 转发失败
      }
    }
  },
  ageNext() {
    if (this.data.age < this.data.ageList.length - 1) {
      this.data.ageLeft = this.data.ageLeft - this.data.value
      this.animation.translateX(this.data.ageLeft, 0).step()
      this.setData({
        ageMove: this.animation.export(),
        age: this.data.age + 1
      })
    }
  },
  agePrevious() {
    if (this.data.age > 0) {
      this.setData({
        age: this.data.age - 1,
      })
      this.data.ageLeft = this.data.ageLeft + this.data.value
      this.animation.translateX(this.data.ageLeft, 0).step()
      this.setData({
        ageMove: this.animation.export()
      })
    }
  },

  touchStart: function(e) {
    let sx = e.touches[0].pageX
    let sy = e.touches[0].pageY
    this.data.touchS = [sx, sy]
  },
  touchMove: function(e) {
    let sx = e.touches[0].pageX;
    let sy = e.touches[0].pageY;
    this.data.touchE = [sx, sy]
  },
  ageTouchEnd: function(e) {
    let start = this.data.touchS
    let end = this.data.touchE
    if (start[0] < end[0] - this.data.coordinates/2) {
      let val = Math.round((end[0] - start[0]) / this.data.coordinates)
      this.data.ageLeft += this.data.value * val
      this.animation.translateX(this.data.ageLeft, 0).step()
      this.setData({
        ageMove: this.animation.export(),
        age: this.data.age - val
      })
    } else if (start[0] > end[0] + this.data.coordinates / 2) {
      let val = Math.round((start[0] - end[0]) / this.data.coordinates)
      this.data.ageLeft -= this.data.value * val
      this.animation.translateX(this.data.ageLeft, 0).step()
      this.setData({
        ageMove: this.animation.export(),
        age: this.data.age + val
      })
    }
  },

  statureNext() {
    if (this.data.stature < this.data.statureList.length - 1) {
      this.setData({
        stature: this.data.stature + 1,
      })
      this.data.statureLeft = this.data.statureLeft - this.data.value
      this.animation.translateX(this.data.statureLeft, 0).step()
      this.setData({
        statureMove: this.animation.export()
      })
    }
  },
  staturePrevious() {
    if (this.data.stature > 0) {
      this.setData({
        stature: this.data.stature - 1,
      })
      this.data.statureLeft = this.data.statureLeft + this.data.value
      this.animation.translateX(this.data.statureLeft, 0).step()
      this.setData({
        statureMove: this.animation.export()
      })
    }
  },
  statureTouchEnd: function (e) {
    let start = this.data.touchS
    let end = this.data.touchE
    if (start[0] < end[0] - this.data.coordinates / 2) {
      let val = Math.round((end[0] - start[0]) / this.data.coordinates)
      this.data.statureLeft += this.data.value * val
      this.animation.translateX(this.data.statureLeft, 0).step()
      this.setData({
        statureMove: this.animation.export(),
        stature: this.data.stature - val
      })
    } else if (start[0] > end[0] + this.data.coordinates / 2) {
      let val = Math.round((start[0] - end[0]) / this.data.coordinates)
      this.data.statureLeft -= this.data.value * val
      this.animation.translateX(this.data.statureLeft, 0).step()
      this.setData({
        statureMove: this.animation.export(),
        stature: this.data.stature + val
      })
    }
  },
  weightNext() {
    if (this.data.weight < this.data.weightList.length - 1) {
      this.setData({
        weight: this.data.weight + 1,
      })
      this.data.weightLeft = this.data.weightLeft - this.data.value
      this.animation.translateX(this.data.weightLeft, 0).step()
      this.setData({
        weightMove: this.animation.export()
      })
    }
  },
  weightPrevious() {
    if (this.data.weight > 0) {
      this.setData({
        weight: this.data.weight - 1,
      })
      this.data.weightLeft = this.data.weightLeft + this.data.value
      this.animation.translateX(this.data.weightLeft, 0).step()
      this.setData({
        weightMove: this.animation.export()
      })
    }
  },
  weightTouchEnd: function (e) {
    let start = this.data.touchS
    let end = this.data.touchE
    if (start[0] < end[0] - this.data.coordinates / 2) {
      let val = Math.round((end[0] - start[0]) / this.data.coordinates)
      this.data.weightLeft += this.data.value * val
      this.animation.translateX(this.data.weightLeft, 0).step()
      this.setData({
        weightMove: this.animation.export(),
        weight: this.data.weight - val
      })
    } else if (start[0] > end[0] + this.data.coordinates / 2) {
      let val = Math.round((start[0] - end[0]) / this.data.coordinates)
      this.data.weightLeft -= this.data.value * val
      this.animation.translateX(this.data.weightLeft, 0).step()
      this.setData({
        weightMove: this.animation.export(),
        weight: this.data.weight + val
      })
    }
  },

  // 预算资金
  changeMoney(e) {
    this.data.money = e.detail.value
  },
  // 联系人
  changeName(e) {
    this.data.name = e.detail.value
  },
  // 联系电话
  changePhone(e) {
    this.data.phone = e.detail.value
  }
})